package com.hh.properties;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.concurrent.TimeUnit;

/**
 * 京东到家（jddj） 同步service
 * 负责将调度域渠道最终分配库存，同步到京东到家渠道店铺
 */
@Data
@Slf4j
@Component
@ConfigurationProperties(prefix = SyncConfig.PROP_PREFIX)
public class SyncConfig implements InitializingBean {
    /** 本配置文件属性的前缀 */
    public static final String PROP_PREFIX = "openplat";
    /** 成功日志发送到kafka的topic name */
    public static final String SYNC_SUCCESS_LOG_KAFKA_TOPIC_O2O = "sync_success_log_topic_o2o";

    /** 是否在测试，如果是，不会走第三方接口，空跑流程 */
    public boolean inTesting = true;

    /** 京东到家配置 */
    private JddjConfig jddj = new JddjConfig();
    /** 京东闪电送配置 */
    private JdsdsConfig jdsds = new JdsdsConfig();
    /** 天猫闪电送配置 */
    private TmsdsConfig tmsds = new TmsdsConfig();


    private static SyncConfig instance;

    public static SyncConfig getInstance() {
        return instance;
    }


    @Data
    public static class PublicConfig {
        /** apiVersion */
        String apiVersion = "1.0";
        /** appKey */
        String appKey;
        /** url */
        String url;
        /** appSecret */
        String appSecret;
        /** token */
        String token;
        /** 每次请求最多携带的数据条数 */
        Integer batchSize = 50;
        /** 限流时间单位（多条数据） */
        TimeUnit batchRequestLimitUnit;
        /** 限流时间单位值（多条数据） */
        Integer batchRequestLimitValue;
        /** 成功日志保存多久 */
        Integer successLogRetainDays = 7;
    }


    // ----------------------------------------------tmsds 闪电 end


    @Override
    public void afterPropertiesSet() throws Exception {
        instance = this;
        log.info("\n京东、天猫开放平台参数：\n{}", JSON.toJSONString(this, SerializerFeature.PrettyFormat));
    }

    public String changeParam(String key, String value) {
        if (key == null || !key.contains(PROP_PREFIX)) {
            return "key:" + key + "必须包含" + PROP_PREFIX + "前缀";
        }

        Field[] declaredFields = this.getClass().getDeclaredFields();
        Field field = null;
        for (Field declaredField : declaredFields) {
            if (declaredField.isAnnotationPresent(Value.class)) {
                if (declaredField.getAnnotation(Value.class).value().contains(key)) {
                    field = declaredField;
                    break;
                }
            }
        }
        if (field == null) {
            System.setProperty(key, value);
            return "没有找到key：" + key + " 准备设置到SystemProperties中";
        }
        return null;
    }


    @Data
    public static class JddjConfig extends PublicConfig {
        /** 操作人pin */
        String userPin;
        /** 限流时间单位（单条数据） */
        TimeUnit requestLimitUnit = TimeUnit.MINUTES;
        /** 限流时间值（单条数据） */
        Integer requestLimitValue = 1500;
        Integer defaultBatchRequestLimitValue = 300;
        TimeUnit defaultBatchRequestLimitUnit = TimeUnit.MINUTES;

        @Override
        public TimeUnit getBatchRequestLimitUnit() {
            TimeUnit unit = super.getBatchRequestLimitUnit();
            return unit == null ? defaultBatchRequestLimitUnit : unit;
        }

        @Override
        public Integer getBatchRequestLimitValue() {
            Integer value = super.getBatchRequestLimitValue();
            return value == null ? defaultBatchRequestLimitValue : value;
        }
    }


    @Data
    public static class JdsdsConfig extends PublicConfig {
        Integer defaultBatchRequestLimitValue = 1000000;
        TimeUnit defaultBatchRequestLimitUnit = TimeUnit.DAYS;

        @Override
        public TimeUnit getBatchRequestLimitUnit() {
            TimeUnit unit = super.getBatchRequestLimitUnit();
            return unit == null ? defaultBatchRequestLimitUnit : unit;
        }

        @Override
        public Integer getBatchRequestLimitValue() {
            Integer value = super.getBatchRequestLimitValue();
            return value == null ? defaultBatchRequestLimitValue : value;
        }
    }

    @Data
    public static class TmsdsConfig extends PublicConfig {
        Long ownerId;
        Integer defaultBatchRequestLimitValue = 1000000;
        TimeUnit defaultBatchRequestLimitUnit = TimeUnit.DAYS;

        @Override
        public TimeUnit getBatchRequestLimitUnit() {
            TimeUnit unit = super.getBatchRequestLimitUnit();
            return unit == null ? defaultBatchRequestLimitUnit : unit;
        }

        @Override
        public Integer getBatchRequestLimitValue() {
            Integer value = super.getBatchRequestLimitValue();
            return value == null ? defaultBatchRequestLimitValue : value;
        }
    }
}

